package cn.iocoder.yudao.module.member.service.score;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.score.vo.ScoreCreateReqVO;
import cn.iocoder.yudao.module.member.controller.admin.score.vo.ScoreExportReqVO;
import cn.iocoder.yudao.module.member.controller.admin.score.vo.ScorePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.score.vo.ScoreUpdateReqVO;
import cn.iocoder.yudao.module.member.convert.score.ScoreConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.score.ScoreDO;
import cn.iocoder.yudao.module.member.dal.mysql.score.ScoreMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.SCORE_NOT_EXISTS;

/**
 * 用户积分记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ScoreServiceImpl implements ScoreService {

    @Resource
    private ScoreMapper scoreMapper;

    @Override
    public Long createScore(ScoreCreateReqVO createReqVO) {
        // 插入
        ScoreDO score = ScoreConvert.INSTANCE.convert(createReqVO);
        scoreMapper.insert(score);
        // 返回
        return score.getId();
    }

    @Override
    public void updateScore(ScoreUpdateReqVO updateReqVO) {
        // 校验存在
        validateScoreExists(updateReqVO.getId());
        // 更新
        ScoreDO updateObj = ScoreConvert.INSTANCE.convert(updateReqVO);
        scoreMapper.updateById(updateObj);
    }

    @Override
    public void deleteScore(Long id) {
        // 校验存在
        validateScoreExists(id);
        // 删除
        scoreMapper.deleteById(id);
    }

    private void validateScoreExists(Long id) {
        if (scoreMapper.selectById(id) == null) {
            throw exception(SCORE_NOT_EXISTS);
        }
    }

    @Override
    public ScoreDO getScore(Long id) {
        return scoreMapper.selectById(id);
    }

    @Override
    public List<ScoreDO> getScoreList(Collection<Long> ids) {
        return scoreMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ScoreDO> getScorePage(ScorePageReqVO pageReqVO) {
        return scoreMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ScoreDO> getScoreList(ScoreExportReqVO exportReqVO) {
        return scoreMapper.selectList(exportReqVO);
    }

}
